package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.dao.PaasFormPaasSubDataDao;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasFormSubDataEsModel;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.constant.SubFormTableNameConstant;
import com.xbongbong.saas.domain.entity.CustomerSubDataEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.CustomerSubDataModel;
import com.xbongbong.saas.model.IndexTypeModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2020/9/4 13:51
 * @since
 * @version
 */
@Service("customerSubDataModel")
public class CustomerSubDataModelImpl implements CustomerSubDataModel<CustomerSubDataEntity> {

    @Resource
    private PaasFormPaasSubDataDao paasFormPaasSubDataDao;
    @Resource
    private PaasFormSubDataEsModel paasFormSubDataEsModel;
    @Resource
    private IndexTypeModel indexTypeModel;

    @Override
    public Integer insert(CustomerSubDataEntity entity) throws XbbException {
        StandardizeDataUtil.standardizeNotString(entity);
        int insert = paasFormPaasSubDataDao.insert(entity, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);

        IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(entity.getCorpid(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

        paasFormSubDataEsModel.insert(entity, indexTypeEnum);
        return insert;
    }

    @Override
    public Integer update(CustomerSubDataEntity entity) throws XbbException {
        StandardizeDataUtil.standardizeNotString(entity);
        int update = paasFormPaasSubDataDao.update(entity, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
        IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(entity.getCorpid(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

        paasFormSubDataEsModel.update(entity, indexTypeEnum);
        return update;
    }

    @Override
    public List<CustomerSubDataEntity> findEntitys(Map<String, Object> param) {
        return (List<CustomerSubDataEntity>) paasFormPaasSubDataDao.findEntitys(param, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return paasFormPaasSubDataDao.getEntitysCount(param, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer insertBatch(List<CustomerSubDataEntity> customerSubDataEntities) throws XbbException {
        if (CollectionUtils.isEmpty(customerSubDataEntities)) {
            return 0;
        }
        StandardizeDataUtil.standardize4SubEntityList(customerSubDataEntities);
        Integer integer = paasFormPaasSubDataDao.insertBatch(customerSubDataEntities, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
        PaasFormSubDataEntity paasFormSubDataEntity = customerSubDataEntities.get(0);
        IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(paasFormSubDataEntity.getCorpid(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

        paasFormSubDataEsModel.insertBatch(customerSubDataEntities, indexTypeEnum);
        return integer;
    }

    @Override
    public List<CustomerSubDataEntity> getByDataId(Long dataId, String corpid) {
        return (List<CustomerSubDataEntity>) paasFormPaasSubDataDao.getByDataId(dataId, corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
    }

    @Override
    public PaasFormSubDataEntity getByKey(Long id, String corpid) {
        return  paasFormPaasSubDataDao.getByKey(id, corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
    }

    @Override
    public List<CustomerSubDataEntity> getByDataIds(List<Long> dataIds, String corpid, DelEnum delEnum) {
        return (List<CustomerSubDataEntity>) paasFormPaasSubDataDao.getByDataIds(dataIds, corpid, delEnum.getDel(), SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> updateDataList, String corpid) throws XbbException {
        if (CollectionUtils.isEmpty(updateDataList)) {
            return 0;
        }
        StandardizeDataUtil.standardizeList(updateDataList);
        Integer integer = paasFormPaasSubDataDao.updateBatch(updateDataList, corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
        if (!Objects.equals(0, integer)) {
            paasFormPaasSubDataDao.updateBatchTime(updateDataList, corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
            IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

            paasFormSubDataEsModel.updateBatch(updateDataList, indexTypeEnum, WriteRequest.RefreshPolicy.NONE);
        }
        return integer;
    }

    @Override
    public void restoreBatch(List<Long> dataIds, String corpid) throws XbbException {
        if (CollectionUtils.isEmpty(dataIds)) {
            return;
        }
        List<CustomerSubDataEntity> entities = getByDataIds(dataIds, corpid, DelEnum.DELETE_SUBFORM);
        paasFormPaasSubDataDao.restoreBatch(dataIds, corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
        IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

        paasFormSubDataEsModel.deleteBatch(corpid, entities, indexTypeEnum, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
    }

    @Override
    public Integer deleteBatchByDataId(List<Long> dataIds, String corpid, DelEnum delEnum) throws XbbException {
        if (CollectionUtils.isEmpty(dataIds)) {
            return 0;
        }
        List<CustomerSubDataEntity> entities = getByDataIds(dataIds, corpid, DelEnum.NORMAL);
        Integer integer = paasFormPaasSubDataDao.deleteBatchByDataId(dataIds, corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA, delEnum.getDel());
        IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

        paasFormSubDataEsModel.deleteBatch(corpid, entities, indexTypeEnum, WriteRequest.RefreshPolicy.NONE, delEnum);
        return integer;
    }

    @Override
    public Integer deleteBatch(String corpid, List<CustomerSubDataEntity> customerSubDataEntities, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        if (CollectionsUtil.isEmpty(customerSubDataEntities)) {
            return 0;
        }
        List<Long> ids = new ArrayList<>();
        for (PaasFormSubDataEntity v : customerSubDataEntities) {
            ids.add(v.getId());
        }
        Integer result = paasFormPaasSubDataDao.deleteById(ids, corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
        IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

        paasFormSubDataEsModel.deleteBatch(corpid, customerSubDataEntities, indexTypeEnum, refreshPolicy, DelEnum.DELETE);
        return result;
    }


    @Override
    public Integer deleteByDataId(Long dataId, String corpid) throws XbbException {
        List<CustomerSubDataEntity> customerSubDataEntities = getByDataId(dataId, corpid);
        Integer result = paasFormPaasSubDataDao.deleteByDataId(dataId, corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);
        IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

        paasFormSubDataEsModel.deleteBatch(corpid, customerSubDataEntities,indexTypeEnum, WriteRequest.RefreshPolicy.NONE, DelEnum.DELETE_SUBFORM);
        return result;
    }

    @Override
    public Integer deleteByKey(Long id, String corpid) throws XbbException {
        PaasFormSubDataEntity paasFormSubDataEntity = getByKey(id, corpid);
        Integer result = paasFormPaasSubDataDao.deleteById(Collections.singletonList(id), corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);

        IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

        paasFormSubDataEsModel.deleteBatch(corpid, Collections.singletonList(paasFormSubDataEntity), indexTypeEnum, WriteRequest.RefreshPolicy.NONE, DelEnum.DELETE_SUBFORM);
        return result;
    }

    @Override
    public Integer updateDel(List<CustomerSubDataEntity> updateList, String corpid, WriteRequest.RefreshPolicy refreshPolicy, DelEnum delEnum) throws XbbException {
        if (CollectionsUtil.isEmpty(updateList)) {
            return 0;
        }
        List<Long> ids = new ArrayList<>();
        for (PaasFormSubDataEntity v : updateList) {
            ids.add(v.getId());
        }
        IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

        Integer result = paasFormPaasSubDataDao.updateDelByIdList(ids, corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA, delEnum.getDel());
        paasFormSubDataEsModel.deleteBatch(corpid, updateList, indexTypeEnum, refreshPolicy, delEnum);
        return result;
    }

    @Override
    public Integer updateBatchByEntity(List<? extends PaasFormSubDataEntity> list, String corpid) throws XbbException {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        StandardizeDataUtil.standardize4SubEntityList(list);
        Integer integer = paasFormPaasSubDataDao.updateBatchByEntity(list, corpid, SubFormTableNameConstant.TB_SAAS_CUSTOMER_SUB_DATA);

        IndexTypeEnum indexTypeEnum = indexTypeModel.getCustomerSubIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

        paasFormSubDataEsModel.updateBatchByEntity(list, indexTypeEnum, WriteRequest.RefreshPolicy.NONE);
        return integer;
    }
}
